/*
 * Copyright 2009 Colin Prinn
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package net.recursv.motific.at.scene;

import net.recursv.motific.at.command.AtScriptCompilationException;
import net.recursv.motific.utils.Convert;
import net.recursv.motific.utils.StringList;

/**
 * Created by: recursv
 * Date: 29/08/2009
 * Time: 8:23:54 PM
 */
public class SceneRequirement {
    private final String _original;
    private String _constructTitle;
    private String _text;
    private int _locationX = -1;
    private int _locationY = -1;
    private int _height = -1;
    private int _width = -1;
    private int _color = -1;


    public SceneRequirement(String original) {
        String s = _original = original.trim();
        StringBuffer b = new StringBuffer();
        boolean inConstruct = false;
        String specifier = null;

        for (int index = 0; index < s.length(); index++) {
            char c = s.charAt(index);

            switch (c) {
                case '$':
                    if (inConstruct) {
                        _constructTitle = b.toString();
                        b.delete(0, b.length());
                        inConstruct = false;
                    } else {
                        inConstruct = true;
                    }
                    break;

                case '(':
                    specifier = b.toString().trim();
                    b.delete(0, b.length());
                    break;

                case ')':
                    assignSpecificValue(specifier, b.toString().trim());
                    specifier = null;
                    b.delete(0, b.length());
                    break;

                case ' ':
                    if (_constructTitle == null) {
                        _constructTitle = b.toString().trim();
                        b.delete(0, b.length());
                        break;
                    }

                default:
                    b.append(c);
            }
        }
    }

    private void assignSpecificValue(String specifier, String value) {
        if (specifier.equals("at"))
            assignLocation(value);
        else if (specifier.equals("text"))
            assignText(value);
        else if (specifier.equals("dim"))
            assignDimensions(value);
        else if (specifier.equals("color"))
            assignColor(value);
    }

    private void assignColor(String value) {
        if (value.startsWith("0x")) {
            _color = Convert.stringToInteger(value);
        } else {
            try {
                _color = Integer.parseInt(value);
            } catch (NumberFormatException e) {
                _color = ConstructManager.getColor(value);
            }
        }
    }

    private void assignDimensions(String value) {
        StringList stringList = new StringList(value, ',');
        if (stringList.size() != 2) {
            throw new AtScriptCompilationException("at(X,Y)");
        } else {
            _width = Integer.parseInt(stringList.get(0));
            _height = Integer.parseInt(stringList.get(1));
        }
    }

    private void assignText(String value) {
        _text = value;
    }

    private void assignLocation(String value) {
        StringList stringList = new StringList(value, ',');
        if (stringList.size() != 2) {
            throw new AtScriptCompilationException("at(X,Y)");
        } else {
            _locationX = Integer.parseInt(stringList.get(0));
            _locationY = Integer.parseInt(stringList.get(1));
        }
    }

    public String getTitle() {
        return _constructTitle;
    }

    public int getX() {
        return _locationX;
    }

    public int getY() {
        return _locationY;
    }

    public String getText() {
        return _text;
    }

    public String getOriginal() {
        return _original;
    }

    public boolean specifiesLocation() {
        return _locationX != -1 && _locationY != -1;
    }

    public int getHeight() {
        return _height;
    }

    public boolean specifiesDimensions() {
        return _width != -1 && _height != -1;
    }

    public int getWidth() {
        return _width;
    }

    public boolean specifiesColour() {
        return _color != -1;
    }

    public int getColor() {
        return _color;
    }

    public void setColor(int color) {
        _color = color;
    }

    public boolean matches(GraphicsTask task) {
        if (getTitle().equals(task.getName())) {
            if (specifiesDimensions() && (getWidth() != task.getWidth() || getHeight() != task.getHeight()))
                return false;
            if (specifiesColour() && (getColor() != task.getColor()))
                return false;
            if (specifiesLocation() && (getX() != task.getX() || getY() != task.getY()))
                return false;
            if (specifiesText() && !getText().equals(task.getText()))
                return false;
            return true;
        } else
            return false;
    }

    public boolean specifiesText() {
        return _text != null && _text.length() > 0;
    }

}
